/*
 *
 */
package custom.base.utils;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.widget.Toast;

import custom.base.R;

import java.io.File;
import java.io.IOException;

/**
 * 存储卡管理类.
 *
 * @author MaXingliang
 */
public class StorageUtil {

    /**
     * The Constant TAG.
     */
    private final String TAG = getClass().getSimpleName();

    /**
     * The Constant EXTERNAL_STORAGE.
     */
    public static final String EXTERNAL_STORAGE = Environment
            .getExternalStorageDirectory().toString();

    /**
     * 需要权限的外部存储根目录
     */
    public static String EXTERNAL_ROOT_DIR = EXTERNAL_STORAGE + File.separator + "DiskManager";
    /**
     * 无需权限的内部存储根目录
     */
    public static String CACHE_ROOT_DIR = EXTERNAL_STORAGE;

    /**
     * 缓存图片文件夹.
     */
    private static String IMAGE_DIR = EXTERNAL_ROOT_DIR + File.separator + "image";

    /**
     * 日志文件夹.
     */
    private static String LOG_DIR = EXTERNAL_ROOT_DIR + File.separator + "log";
    /**
     * voice
     */
    private static String VOICE_DIR = EXTERNAL_ROOT_DIR + File.separator + "voice";
    /**
     * temp
     */
    private static String TEMP_DIR = EXTERNAL_ROOT_DIR + File.separator + "temp";
    /**
     * temp
     */
    private static String DOWNLOAD_DIR = EXTERNAL_ROOT_DIR + File.separator + "download";
    /**
     * 办公文件
     */
    private static String DOWNLOAD_OFFICE_DIR = EXTERNAL_ROOT_DIR + File.separator + "officeFile";
    /**
     * 视频文件
     */
    private static String DOWNLOAD_VIDEO = EXTERNAL_ROOT_DIR + File.separator + "video";
    /**
     * 保存的文件地址
     */
    private static String SAVED_FILE = EXTERNAL_ROOT_DIR + File.separator + "savedFile";
    /**
     * 保存的文件地址
     */
    private static String ACCOUNT = EXTERNAL_ROOT_DIR + File.separator + "account";
    /**
     * 保存的文件地址
     */
    private static String APP_WEB = EXTERNAL_ROOT_DIR + File.separator + "appweb";

    //==================文件类型

    /**
     * The Constant DIR_TYPE_IMAGE.
     */
    public static final int DIR_TYPE_IMAGE = 1;

    /**
     * The Constant DIR_TYPE_LOG.
     */
    public static final int DIR_TYPE_LOG = 1 << 1;

    /**
     * 声音
     */
    public static final int DIR_TYPE_VOICE = 1 << 2;

    /**
     * 临时文件
     */
    public static final int DIR_TYPE_TEMP = 1 << 3;
    /**
     * 下载文件
     */
    public static final int DIR_TYPE_DOWNLOAD = 1 << 4;
    /**
     * 官方文档下载文件
     */
    public static final int DIR_TYPE_OFFICE_FILE = 1 << 5;
    /**
     * 视频文件
     */
    public static final int DIR_TYPE_VIDEO = 1 << 6;
    /**
     * 保存的文件地址
     */
    public static final int DIR_TYPE_SAVED_FILE = 1 << 7;
    /**
     * 保存的账户地址
     */
    public static final int DIR_TYPE_ACCOUNT = 1 << 8;
    /**
     * 保存的账户地址
     */
    public static final int DIR_TYPE_APP_WEB = 1 << 9;

    /**
     * 临时测试文件名
     */
    public static final String TMP_JPG_NAME = "dz_tmp_img.jpg";

    /**
     * Instantiates a new storage util.
     */
    StorageUtil() {
    }

    /**
     * 得到应用缓存文件夹路径
     */
    public static String getCacherDir(Context context) {
        File file = context.getCacheDir();
        return file.getAbsolutePath();
    }

    /**
     * 把所有目录切换成缓存的目录
     */
    public static String initRootDir(Context context) {
        boolean externalStorageAvailable = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

        String cachePath;
        if (externalStorageAvailable) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        CACHE_ROOT_DIR = cachePath;

        /**
         * 缓存图片文件夹.
         */
        IMAGE_DIR = CACHE_ROOT_DIR + File.separator + "image";

        /**
         * 日志文件夹.
         */
        LOG_DIR = CACHE_ROOT_DIR + File.separator + "log";
        /**
         * voice
         */
        VOICE_DIR = CACHE_ROOT_DIR + File.separator + "voice";
        /**
         * temp
         */
        TEMP_DIR = CACHE_ROOT_DIR + File.separator + "temp";
        /**
         * temp
         */
        DOWNLOAD_DIR = CACHE_ROOT_DIR + File.separator + "download";
        /**
         * 办公文件
         */
        DOWNLOAD_OFFICE_DIR = CACHE_ROOT_DIR + File.separator + "officeFile";
        /**
         * 视频文件
         */
        DOWNLOAD_VIDEO = CACHE_ROOT_DIR + File.separator + "video";
        /**
         * 保存的文件地址
         */
        SAVED_FILE = CACHE_ROOT_DIR + File.separator + "savedFile";
        /**
         * 保存的文件地址
         */
        ACCOUNT = CACHE_ROOT_DIR + File.separator + "account";
        /**
         * 保存的文件地址
         */
        APP_WEB = CACHE_ROOT_DIR + File.separator + "appweb";

        return cachePath;
    }

    /**
     * Checks for external storage.
     *
     * @return true, if successful
     */
    public static boolean hasExternalStorage() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    /**
     * Gets the dir by type.
     *
     * @param type the type
     * @return the dir by type
     */
    public static File getDirFileByType(int type) {
        return new File(getDirByType(type));
    }

    /**
     * Gets the dir by type.
     *
     * @param type the type
     * @return the dir by type
     */
    public static String getDirByType(int type) {
        String dir = EXTERNAL_STORAGE;
        String filePath = "";
        switch (type) {
            case DIR_TYPE_IMAGE: {
                filePath = IMAGE_DIR;
                break;
            }
            case DIR_TYPE_LOG: {
                filePath = LOG_DIR;
                break;
            }
            case DIR_TYPE_VOICE: {
                filePath = VOICE_DIR;
                break;
            }
            case DIR_TYPE_TEMP: {
                filePath = TEMP_DIR;
                break;

            }
            case DIR_TYPE_DOWNLOAD: {
                filePath = DOWNLOAD_DIR;
                break;

            }
            case DIR_TYPE_OFFICE_FILE: {
                filePath = DOWNLOAD_OFFICE_DIR;
                break;

            }
            case DIR_TYPE_VIDEO: {
                filePath = DOWNLOAD_VIDEO;
                break;

            }
            case DIR_TYPE_SAVED_FILE: {
                filePath = SAVED_FILE;
                break;

            }
            case DIR_TYPE_ACCOUNT: {
                filePath = ACCOUNT;
                break;

            }
            case DIR_TYPE_APP_WEB: {
                filePath = APP_WEB;
                break;

            }
        }
        File fileNomedia = new File(EXTERNAL_ROOT_DIR + File.separator + " .nomedia");//告诉系统无需扫描本文件夹
        if (!fileNomedia.exists()) {
            try {
                fileNomedia.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        File file = new File(filePath);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }
        if (file.exists()) {
            if (file.isDirectory()) {
                dir = file.getPath();
            }
        } else {
            // 文件没创建成功，可能是sd卡不存在，但是还是把路径返回
            dir = filePath;
        }
        return dir;
    }

    /**
     * 是否插入sdcard.
     *
     * @return true, if is sD card exist
     */
    public static boolean isSDCardExist() {
        boolean sdCardExist = false;
        sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        return sdCardExist;
    }

    /**
     * 判断存储空间是否足够.
     *
     * @param needSize the need size
     * @return true, if successful
     */
    public static boolean checkExternalSpace(float needSize, Context context) {
        boolean flag = false;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());
            long blockSize = sf.getBlockSize();
            long availCount = sf.getAvailableBlocks();
            long restSize = availCount * blockSize;
            if (restSize > needSize) {
                flag = true;
            } else {
                Toast.makeText(context, R.string.sd_smallsize,
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, R.string.no_sdcard, Toast.LENGTH_SHORT)
                    .show();
        }
        return flag;
    }

    /**
     * 得到项目源文件夹地址
     */
    public static String getProjectDir() {
        return EXTERNAL_ROOT_DIR;
    }
}
